package app.common.utils.history;

import app.common.utils.WdtUtils;
import app.common.utils.cjt.CJtUtils;
import app.common.utils.cjt.ao.OtherDispatchCreateAo;
import app.common.utils.cjt.ao.OtherQueryQueryWithDetailAo;
import app.common.utils.cjt.ao.ToOtherDispatchCreateAoDto;
import app.common.utils.cjt.ao.WmsStockPdQueryStockPdOutDetailAo;
import app.common.utils.cjt.ao.WmsStockoutTransferQueryWithDetailAo;
import app.temporary.Error;
import com.alibaba.fastjson.JSON;
import lombok.SneakyThrows;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * wdt ck
 * @author feizns
 * @since 2023/07/21
 */
public interface WdtToCK {

    /**
     * 错误
     */
    List<Error<OtherDispatchCreateAo.Dto>> errors = new ArrayList<>();

    AtomicInteger successCount = new AtomicInteger(0);

    static void main(String[] args) {
        LocalDate startDate = LocalDate.of(2023, 1, 1);
        LocalDate endDate = LocalDate.of(2023, 1, 26);
        WdtToCK.wdtToCk(startDate, endDate);
        System.out.println(successCount);
        System.out.println("传输完毕");
        System.out.println("错误列表：");
        errors.forEach(error -> {
            System.out.println();
            System.out.println(JSON.toJSONString(error.getDto()));
            System.out.println(error.getE().getMessage());
            System.out.println();
        });
//        System.out.println(errors);
    }

    /**
     * wdt其他入库转畅捷通其他出库
     * @param startDate 开始日期
     * @param endDate 结束日期
     */
    @SneakyThrows
    static void wdtToCk(LocalDate startDate, LocalDate endDate) {
        LocalDateTime startTime = startDate.atStartOfDay();
        LocalDateTime endTime = endDate.atStartOfDay();
        //其他出库单创建
        WdtUtils.otherQueryQueryWithDetail(new OtherQueryQueryWithDetailAo(startTime, endTime), order -> {
            if ( order.getOperator_name() != null && !order.getOperator_name().trim().isEmpty() )
                execute(order);
        });
        //调拨出库单创建
        WdtUtils.wmsStockoutTransferQueryWithDetail(new WmsStockoutTransferQueryWithDetailAo(startTime, endTime), WdtToCK::execute);
//        生产出库单
//        WdtUtils.wmsStockoutProcessQueryWithDetail(new WmsStockoutProcessQueryWithDetailAo(startTime, endTime), WdtToCK::execute);
        //盘点出库单
        WdtUtils.wmsStockPdQueryStockPdOutDetail(new WmsStockPdQueryStockPdOutDetailAo(startTime, endTime), WdtToCK::execute);
    }

    private static void execute(ToOtherDispatchCreateAoDto order) {
        try {
//            System.out.println(order.toOtherDispatchCreateAo());
            successCount.incrementAndGet();
            CJtUtils.otherDispatchCreate(order.toOtherDispatchCreateAo());
        } catch (Exception e) {
            errors.add(new Error<>(order.toOtherDispatchCreateAo(), e));
            e.printStackTrace();
        }
    }

}
